﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SchoolCardSystem
{
    public partial class ResetPass : System.Web.UI.Page
    {
        String userName;
        //定义数据库信息
        MySqlConnection connection = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
                
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取修改的用户
            userName = Session["findName"].ToString();
        }
        //修改密码
        protected void confirm_Click(object sender, EventArgs e)
        {
            
            if (passText.Text.Trim() == "")
            {
                errorText.Text = "请输入新的密码";
                errorText.Style["display"] = "inline";
            }
            else if (comfirmText.Text.Trim() == "")
            {
                errorText.Text = "请确认密码";
                errorText.Style["display"] = "inline";
            }
            else {
                String firstPass = passText.Text.Trim();
                String secondPass = comfirmText.Text.Trim();
                //密码不一致
                if (firstPass != secondPass)
                {
                    errorText.Text = "两次密码不一致";
                    errorText.Style["display"] = "inline";
                }
                //成功修改
                else {//未验证用户的新密码与旧密码是否一致
                    //打开数据库
                    connection.Open();
                    //更新语句
                    string sqlstr = @"update user set userPass=@newPass where userName=@userName";
                    //Command 更新语句
                    MySqlCommand update = new MySqlCommand(sqlstr, connection);
                    //设置@pass/@userId的类型
                    update.Parameters.Add(new MySqlParameter("@newPass", MySqlDbType.Double, 9));
                    update.Parameters.Add(new MySqlParameter("@userName", MySqlDbType.VarChar, 11));
                    update.Parameters["@userName"].Value = userName;
                    //定义command 更新后密码
                    update.Parameters["@newPass"].Value = firstPass;
                    //成功后显示提示框
                    errorText.Style["display"] = "inline";
                    errorText.Text = "修改成功,点击跳转";
                    //去登陆按钮
                    loginBtn.Style["display"] = "inline";
                    //执行语句
                    update.ExecuteNonQuery();
                    connection.Close();
                }
            }
        }
        //取消按钮
        protected void reset_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

        protected void loginBtn_Click(object sender, EventArgs e)
        {
            Session["name"] = userName;
            Response.Redirect("index.aspx");
        }
    }
}